<!-- 添加模板 -->
<template>
  <el-dialog
    class="black-dialog"
    title="编辑"
    :visible.sync="dialogVisible"
    :destroy-on-close="true"
    width="420px"
    v-loading="dialogLoading"
    :before-close="handleClose">
    <div>
      <el-form ref="form" :model="formData" :rules="rules" :inline="true" class="ky-form-inline ky-form-inline-sm dialog-form" label-width="136px">
        <!--  <el-form-item label="医院名称:" prop="hospitalCode">
          <el-select size="small" class="param-type_input" v-model="formData.hospitalCode" placeholder="请选择" @change="hospitalChange">
            <el-option
              v-for="(item,index) in mx_hospitalList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item prop="templateName" class="auto-width">
          <template slot="label">
            <span>模板名称:</span>
            <el-popover
              placement="top-start"
              title=""
              width="240"
              trigger="hover"
              content="模板名称要与维护的C1report模板名称一致">
              <i class="el-icon-question pointer" slot="reference"></i>
            </el-popover>
          </template>
          <el-input prefix-icon="el-icon-search" v-model="formData.templateName" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="部门名称:" prop="dep" class="auto-width">
          <el-select size="small" class="param-type_input" v-model="formData.dep" @change="handleChange()" placeholder="请选择">
            <el-option
              v-for="(item,index) in scheduledDepOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查设备:" prop="modalityType" class="auto-width">
          <el-select size="small" class="param-type_input" clearable v-model="formData.modalityType" placeholder="请选择">
            <el-option
              v-for="(item,index) in modalityOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="患者类型:" prop="patType" class="auto-width">
          <el-select size="small" class="param-type_input" clearable v-model="formData.patType" placeholder="请选择">
            <el-option
              v-for="(item,index) in patTypeOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="pageType" class="auto-width">
          <template slot="label">
            <span>纸张类型:</span>
            <el-popover
              placement="top-start"
              title=""
              width="240"
              trigger="hover"
              content="请在数据字典中维护好纸张类型(注释: 不要用中文)">
              <i class="el-icon-question pointer" slot="reference"></i>
            </el-popover>
          </template>
          <el-select size="small" class="param-type_input" clearable v-model="formData.pageType" placeholder="请选择">
            <el-option
              v-for="(item,index) in pageTypeOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="save">确 定</el-button>
      </div>
    </div></el-dialog>
</template>

<script>
// import paginationCont from '@/components/Pagination/index'
import { getDepList, getparamtersettings, getModalityList, getItemGroupList } from '@/api/api.js'
import { Options } from '@/utils/constant'
export default {
  name: '',
  mixins: [],
  components: { },
  props: {
    addVisible: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      String,
      default: ''
    },
    nodeData: {
      Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialogVisible: false,
      scheduledDepOptions: [],
      patTypeOptions: Options.patientTypeItemOptions.map(item => item.label),
      pageTypeOptions: [],
      formData: {
        hospitalCode: '',
        dep: '',
        patType: '',
        modalityType: '',
        templateName: '',
        pageType: ''
      },
      ruleForm: {
        autoBanDay: null,
        type: '',
        banDesc: ''
      },
      typeOptions: [],
      modalityOptions: [],
      totalCount: 0,
      pageSize: 10,
      currentPage: 1,
      loading: false,
      dialogLoading: false,
      tableData: [],
      timesOptions: [],
      multipleSelection: [],
      qutpatientRefundForm: false,
      rules: {
        dep: { required: true, message: '部门名称必填', trigger: ['blur', 'change'] },
        templateName: { required: true, message: '模板名称必填', trigger: ['blur', 'change'] }
      }
    }
  },
  watch: {
    addVisible(val) {
      this.dialogVisible = val
      if (val) {
        this.formData = JSON.parse(JSON.stringify(this.nodeData))
      }
    },
    hospitalCode: {
      handler(val) {
        this.formData.hospitalCode = val || this.$store.state.user.hospitalCode
        this.hospitalChange()
        this.timeout01 = setTimeout(() => {
          this.getModalityOptions()
        }, 600)
      },
      immediate: false
    }
  },
  computed: {},
  created() {

  },
  mounted() {
    // this.$getHospitalList()
  },
  methods: {
    async handleChange() {
      this.modalityOptions = []
      this.formData.modality = ''
      try {
        this.getModalityOptions()
      } catch (err) {
        console.log(err)
      }
    },
    getModalityOptions() {
      /* 检查设备列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode || this.$store.state.user.hospitalCode,
        dep: this.formData.dep
      }
      return new Promise(async (resolve) => {
        await getModalityList(params).then((res) => {
          if (res.code === 10000) {
            this.modalityOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getAppConfig() {
      const userId = this.$store.state.user.id
      const params = {
        hospitalCode: this.hospitalCode,
        userId: userId
      }
      getparamtersettings(params).then(res => {
        if (res && res.code === 10000) {
          let configInfo = null
          if (res.content.hospitalParameterContent) {
            configInfo = res.content.hospitalParameterContent
          }
          if (res.content.userParameterContent && this.configType === 'user') {
            configInfo = res.content.userParameterContent
          }
          if (configInfo) {
            const _info = JSON.parse(configInfo)
            this.qutpatientRefundForm = _info.baseConfig.qutpatientRefundForm
          }
        }
      })
    },
    getScheduledDepOptions() {
      this.getAppConfig()
      /* 检查部门列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode
      }
      getDepList(params).then((res) => {
        if (res.code === 10000) {
          console.log('this.qutpatientRefundForm===>', this.qutpatientRefundForm)
          this.scheduledDepOptions = this.qutpatientRefundForm === true ? res.content.list.concat(['退费单']) : res.content.list
          console.log('this.scheduledDepOptions====>', this.scheduledDepOptions)
        }
      })
    },
    getPageTypeOptions() {
      /* 数据字典下项目列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        Type: '纸张类型'
      }
      return new Promise(resolve => {
        getItemGroupList(params).then((res) => {
          if (res.code === 10000) {
            this.pageTypeOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:addVisible', false)
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('save', this.formData)
          this.handleClose()
        } else {
          return false
        }
      })
    },
    hospitalChange() {
      this.getScheduledDepOptions()
      this.getPageTypeOptions()
    }
  },
  beforeDestroy() {
    this.timeout01 && clearTimeout(this.timeout01)
  },
  destroyed() {},
  beforeUpdate() {},
  updated() {}
}
</script>
<style lang='scss' scoped>
.short-input{
    width: 300px;
    margin-right: 23px!important;
    margin-bottom: 5px;
    /* .el-input{
        width: 200px;
    } */
    .word{
        margin-left: 5px;
    }
    /deep/ .el-form-item__content{
        width: 100%;
        .el-select{
            width: 100%;
            .el-input{
                width: 100%;
            }
        }
    }
}
.el-form{
    .el-form-item{
        margin-bottom: 15px;
    }
}
.black-dialog{
  .auto-width{
    display: flex;
      width:100%;
    /deep/ .el-form-item__content{
        width: 100%;
        .el-select{
            width: 100%;
            .el-input{
                width: 100%;
            }
        }
    }
  }
}
.dialog-footer{
    display: flex;
    justify-content: flex-end;
    margin-top: 15px;
}
</style>
